<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use App\Models\SubOrder;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gate;
use App\Exceptions\Admin\PermissionDeniedException;

class SubOrderController extends Controller
{

    public function sub_order_gate()
    {
        if (!Gate::allows('order','')) {
            throw new PermissionDeniedException();
        }//end if()

    }//end func __construce()

    public function eorder(Request $request,SubOrder $subOrder,User $user)
    {
        $this->sub_order_gate();

        $order_type = 1;


        $subOrderQuery = SubOrder::join('order','sub_order.order_id','=','order.order_id')
            ->where('sub_order.order_type',$order_type)
            ->orderBy('order.add_time','desc');


        $filter = [
            'order_type' => $request->get('order_type'),
            'order_sn' => $request->get('order_sn'),
            'consignee'=>$request->get('order_sn'),
            'order_status'=>$request->get('order_status'),
        ];
        //订单类型
        if ($filter['order_type']!=''){
            $subOrderQuery = $subOrderQuery->where('sub_order.order_type',$filter['order_type']);
        }
        //订单号搜索
        if ($filter['order_sn']!=''){
            $subOrderQuery = $subOrderQuery->where('order.order_sn',$filter['order_sn']);
        }
        //收货人搜索
        if ($filter['consignee']!=''){
            $subOrderQuery = $subOrderQuery->where('order.consignee',$filter['consignee']);
        }
        //订单状态
        if ($filter['order_status']!=''){

            $subOrderQuery = $subOrderQuery->where('sub_order.order_status',$filter['order_status']);
        }

        $orderInfo = $subOrderQuery->paginate();

        $orderStatusArr = ['0'=>'订单状态', '1'=>'待支付', '2'=>'待发货', '3'=>'已发货', '4'=>'已取消', '4'=>'已完成'];


        return view('admin.sub_order.index',compact(['filter','orderInfo','orderStatusArr','user','order_type']));

    }


    public function corder(Request $request,SubOrder $subOrder,User $user)
    {

        $this->sub_order_gate();

        $order_type = 2;


        $subOrderQuery = SubOrder::join('order','sub_order.order_id','=','order.order_id')
            ->where('sub_order.order_type',$order_type)

            ->orderBy('order.add_time','desc');


        $filter = [
            'order_type' => $request->get('order_type'),
            'order_sn' => $request->get('order_sn'),
            'consignee'=>$request->get('order_sn'),
            'order_status'=>$request->get('order_status'),
        ];
        //订单类型
        if ($filter['order_type']!=''){
            $subOrderQuery = $subOrderQuery->where('sub_order.order_type',$filter['order_type']);
        }
        //订单号搜索
        if ($filter['order_sn']!=''){
            $subOrderQuery = $subOrderQuery->where('order.order_sn',$filter['order_sn']);
        }
        //收货人搜索
        if ($filter['consignee']!=''){
            $subOrderQuery = $subOrderQuery->where('order.consignee',$filter['consignee']);
        }
        //订单状态
        if ($filter['order_status']!=''){

            $subOrderQuery = $subOrderQuery->where('sub_order.order_status',$filter['order_status']);
        }

        $orderInfo = $subOrderQuery->paginate();

        $orderStatusArr = ['0'=>'订单状态', '1'=>'待支付', '2'=>'待发货', '3'=>'已发货', '4'=>'已取消', '4'=>'已完成'];


        return view('admin.sub_order.index',compact(['filter','orderInfo','orderStatusArr','user','order_type']));

    }


}
